<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/*发送邮件类*/

class mailClass
{
    /**
     * 发送邮件
     * @param $address 邮件地址
     * @param $code 邮件验证码
     */
    public static function sendEmail($address, $code)
    {
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = false;                                 // Enable verbose debug output
            $mail->CharSet = 'UTF-8';
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.qq.com';                           // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = '779635593@qq.com';                 // SMTP username
            $mail->Password = 'secretCode';                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom('779635593@qq.com', 'Zhuo新Web');
            //$mail->addAddress('13792342104@163.com', 'Joe User');     // Add a recipient
            $mail->addAddress($address);               // Name is optional
            //$mail->addReplyTo('13792342104@163.com', 'Information');
            //$mail->addCC('13792342104@163.com');
            //$mail->addBCC('13792342104@163.com');

            //Attachments
            // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = '您正在注册我的网站:';
            //$mail->Body = '您的验证码是' . $code . '。10分钟内有效';
            $mail->Body = <<<TPL
            <!DOCTYPE html>
            <html>
            <head>
            <meta charset="UTF-8">
            <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
               <!--style-->
                <style type="text/css">
                    table.container{box-shadow: 0 0 0.6em #ccc;-webkit-box-shadow: 0 0 0.6em #ccc;-o-box-shadow: 0 0 0.6em #ccc;-moz-box-shadow: 0 0 0.6em #ccc;-ms-box-shadow: 0 0 0.6em #ccc;}
                    body{width: 100%;background-image: url('images/bg.png');background-repeat: repeat;margin: 0;padding: 0;}
                    table{font-size: 14px;border: 0;}
                    @media only screen and (max-width: 800px){.container{width: 754px !important;}} @media only screen and (max-width: 768px){/*------ top header ------*/.header-top{width: 700px;}
                        .header-bg{width: 440px !important;height: 10px !important;}
                        .main-header{line-height: 28px !important;}
                        /*----- --features ---------*/.feature-middle{width: 400px !important;text-align: center !important;}
                        .feature-img{height: 437px !important;}
                        .about-left{width: 275px;}
                        .about-right{width: 289px;}
                        .container{width: 654px !important;}
                        .container-middle{width: 590px !important;}
                        .member{width: 13px;}
                        table.main-img{height: 400px;}} @media only screen and (max-width: 640px){.logo{font-size: 0.85em;}
                        .container{width: 530px !important}
                        .main-img{width: 100% !important;height: 380px !important;}
                        .container-middle{width: 500px !important}
                        .about-left{width: 225px;}
                        .about-right{width: 226px;font-size: 0.98em;}
                        .mainContent{width: 550px;font-size: 0.98em;}
                        .feature{height: 136px;width: 200px;}
                        table.news-info{width: 210px;}
                        table.news-image{width: 282px;}
                        a.logo{font-size: 3em !important;}
                        table.footer{width: 500px;}} @media only screen and (max-width: 480px){.logo{font-size: 0.85em;margin-top: .5em}
                        .container-middle{width: 390px !important;}
                        .about-right{width: 299px}
                        .main-img{width: 450px !important;height: 259px !important;}
                        .feature1{width: 300px;}
                        .container{width: 400px !important;}
                        .feature{height: 185px;width: 417px;text-align: Center;}
                        .gallery2{margin: 2em 0;}
                        .fet{margin-top: 2em;}
                        .wel{font-size: 2em !important;}
                        .top-header-left{font-size: 0.94em;}
                        table.banner-bottom-text{width: 420px;}
                        .mainContent{width: 420px;}
                        table.footer{width: 420px;}
                        td.main-subheader{text-align: Center !important;margin-top: 20px;}
                        table.news-info{width: 100%;}
                        table.news-image{width: 100%;}} @media only screen and (max-width: 375px){.container{width: 350px !important;}
                        table.banner-bottom-text{width: 350px;}
                        .mainContent{width: 350px;}
                        table.footer{width: 350px;}
                        .container-middle{width: 250px !important;}} @media only screen and (max-width: 320px){.logo{font-size: 0.85em;width: 100%;text-align: center;}
                        .container{width: 296px !important;}
                        .main-img{width: 297px !important;height: 198px !important;}
                        .container-middle{width: 265px !important;}
                        .feature{height: 185px;width: 266px;}
                        .feature1{width: 265px;}
                        .about-right{width: 260px;}
                        .gallery1{width: 279px;height: 137px;}
                        .galry1{height: 150px;}
                        .nav{width: 100%;text-align: center;}
                        .top{height: 20px;}
                        .container{width: 350px !important;}
                        table.banner-bottom-text{width: 350px;}
                        .mainContent{width: 350px;}
                        table.footer{width: 350px;}
                        .container-middle{width: 250px !important;}
                        a.logo{font-size: 2.5em !important;}
                        table.banner-bottom-text{width: 280px;}
                        .mainContent{width: 280px;}
                        table.footer{width: 280px;}
                        .container{width: 050px !important;}
                        table.social-icons{text-align: center;width: 69%;}}
                </style>
                <!--style-->
            </head>
            <body>
            <table>
                <tr>
                    <td height="40" class="top"></td>
                </tr>
            </table>
            <table width="800" border="0" cellpadding="0" cellspacing="0" align="center" class="container">
                <tbody>
                <tr>
                    <td>
                        <table width="800" align="center" border="0" cellpadding="0" cellspacing="0" align="center"
                               class="container">
                            <tbody>
                            <!-- Header -->
                            <tr bgcolor="ffffff">
                                <td>
                                    <table border="0" width="650" align="center" cellpadding="0" cellspacing="0"
                                           class="container-middle">
                                        <tr>
                                            <td>
                                                <table border="0" width="650" align="center" cellpadding="0" cellspacing="0"
                                                       class="container-middle">
            
                                                    <tr>
                                                        <td height="22"></td>
                                                    </tr>
            
                                                    <tr class="top-header-left">
                                                        <td align="center">
                                                            <span class="logo" href="#"
                                                               style="font-size:3.5em;color: #69B922;text-decoration:none;font-family:Century Gothic;font-weight:216; letter-spacing:2px;">验证码
                                                               </span>
                                                        </td>
                                                    </tr>
            
                                                </table>
                                            </td>
                                        <tr>
                                            <td height="20"></td>
                                        </tr>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <!--banner-bottom-->
                            <tr>
                            <tr>
                                <td style="background-color:#FFF" height="40"></td>
                            </tr>
                            <td bgcolor="ffffff">
                                <table width="600" border="0" align="center" cellpadding="0" cellspacing="0" class="mainContent">
                                    <tr>
                                        <td align="center" style="color: #E6842D; font-size: 2.5em; font-family: 'Century Gothic';">
                                            {$code}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td height="8"></td>
                                    </tr>
            
                                    <tr>
                                        <td height="15"></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <table width="600" align="center" class="banner-bottom-text">
                                                <tr>
                                                    <td style="color: #655d35; font-size: 1.15em;font-family: Candara;text-align:center;line-height:2em;">
                                                        请在10分钟内填写验证码。
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td height="20"></td>
                                    </tr>
                                    <tr>
                                        <td align="center" valign="top">
                                            <table align="center" border="0" cellpadding="0" cellspacing="0">
                                                <tbody>
                                                <tr>
                                                    <td align="center"
                                                        style="font-size: 1.2em; color: #FB8A2E; font-weight: normal; text-align: center; font-family:Century Gothic;">
            
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>           
                            </tbody>
                        </table>
                    </td>
                </tr>
                </tbody>
            </table>
            </body>
            </html>
TPL;

            $mail->AltBody = '抓紧去填写吧';
            $mail->send();
            return true;
            //echo 'Message has been sent';
        } catch (Exception $e) {
            //echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
            return false;
        }
    }
}